import axios from 'axios';
import store from '../store'
import {ElMessage} from "element-plus";
import router from "@/router";
// 环境的切换
if (process.env.NODE_ENV == 'development') {
    axios.defaults.baseURL = 'https://os-s1.ccwtech.net/ck1/client/v1';//http://api.ptaedu.cn/
} else if (process.env.NODE_ENV == 'debug') {
    axios.defaults.baseURL = '';
} else if (process.env.NODE_ENV == 'production') {
    axios.defaults.baseURL = 'https://os-s1.ccwtech.net/ck1/client/v1';//host:http://192.168.0.165:8000
}
//请求拦截器
axios.interceptors.request.use(
    config => {
        //发送前,处理逻辑
        if (store.state.token) {
            console.log('http-token',store.state.token);
            config.headers.Authorization = store.state.token
        } else {
            // config.headers.Authorization = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzksImNvZGUiOiI3NDk4M09YM0pXSDVLTjBXWUw4REJRN1kzSkMiLCJzY2hvb2xJRCI6MTcsInBsYXRmb3JtIjoid2ViU2lkZSIsImlhdCI6MTY1NDA3NzE2MiwiZXhwIjoxNjU0NjgxOTYyfQ.R9e7sblxi17LyX7Xr4yz6qXpRsZVFOAj58kXH0Ydd7Q"
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    });
//响应拦截器
axios.interceptors.response.use(
    response => {
        if (response.status === 200 || response.status === 201) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    // 服务器状态码不是200的情况
    error => {
        if (error.response.status) {
            switch (error.response.status) {
                case 400:
                    break;
                case 401:
                    console.log('---------');
                    router.push('/login')
                    //this.$cookies.remove('token');
                    //  router.replace({
                    //      path: 'http://study.ptaedu.com/login/?callback=www.baidu.com',
                    //      query: { redirect: router.currentRoute.fullPath }
                    //  });
                    break;
                case 403:
                    ElMessage({
                        message: "登录过期，请重新登录",
                        type: "error",
                        duration: 1000
                    });
                    // 清除token
                    // localStorage.removeItem('token');
                    // store.commit('loginSuccess', null);
                    // // 跳转登录页面，并将要浏览的页面fullPath传过去，登录成功后跳转需要访问的页面
                    // setTimeout(() => {
                    //     //  router.replace({
                    //     //      path: "http://study.ptaedu.com/login/?callback="+localhost+"/login/callback",
                    //     //      query: {
                    //     //          redirect: router.currentRoute.fullPath
                    //     //      }
                    //     //  });
                    // }, 1000);
                    break;
                // 404请求不存在
                case 404:
                    ElMessage({
                        message: "网络请求不存在",
                        type: "error",
                        duration: 1000
                    });
                    break;
                // 其他错误，直接抛出错误提示
                default:
                    ElMessage({
                        message: error.response.data.msg,
                        type: "error",
                        duration: 1000
                    });
            }
            return Promise.reject(error.response);
        }
    }
    );

export function get(url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}

export function post(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}
